<?php

class authsubproxy{
	static $_instance = null;

	public static function getInstance() 
	{
		if (self::$_instance == null) 
		{
			self::$_instance = new authsubproxy();
		}
		return self::$_instance;
	}


	function getAuthSubUrl() 
	{
		$next = 'http://apps.facebook.com/bundundu/';
		$scope = 'http://www.google.com/calendar/feeds/';
		$secure = false;
		$session = true;
		return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, 
		$session);

		//$authSubUrl = getAuthSubUrl();
		//echo "<a href=\"$authSubUrl\">login to your Google account"</a>     
	      
	      
	}
	
	
	function outputCalendarList($client) 
	{
		$gdataCal = new Zend_Gdata_Calendar($client);
		try{
			$calFeed = $gdataCal->getCalendarListFeed();
		}catch (Zend_Gdata_App_Exception $e) {    
			echo "Oops! An error ocured while trying to get your calendars!: <br/>" . $e->getMessage();
			return;
		}

		echo "<table>";
		echo "<th> Title </th>";
		echo "<th> Access Level </th>";
		echo "<th> Color </th>";
		//echo "<th> Updated </th>";
		echo "<th> Summary </th>";
		echo "<th> Timezone </th>";
		
		foreach ($calFeed as $calendar)
		{
			echo "<tr>";
			
			echo "<td> ". $calendar->title . "</td>" ;	
			//echo "<td>" .$calendar->id  ."</td>";
			echo "<td>" . $calendar->getAccessLevel() .  "</td>";
			echo "<td>"	. $calendar->getColor() . "</td>";
			//echo "<td>" .$calendar->updated . "</td>";
			echo "<td>" .$calendar->summary . "</td>";
			//echo "<td>" .$calendar->getHidden() . "</td>";
			//echo "<td>" .$calendar->getSelected() . "</td>";
			echo "<td>" .$calendar->getTimezone() . "</td>";
			

			echo "</tr>";
		}
		echo "</table>";
	} 
	
	
	function outputCalendar($client) 
	{
		$gdataCal = new Zend_Gdata_Calendar($client);
		$query = $gdataCal->newEventQuery();
		$query->setOrderby('starttime');
		$query->setVisibility('private');
		$query->setProjection('full');
		$query->setUser('default');
		$eventFeed = $gdataCal->getCalendarEventFeed($query);
		echo "<table>";

		foreach ($eventFeed as $event)
		{
			foreach ($event->when as $when)
			{
				echo "<tr>";

				echo "<td> <p class='gc_title'>". $event->title->text ." <p/> </td>";
				echo "<td> <a href= ". $event->id->text ." > Details </a> </td>";
				echo "<td> Starts: " . $when->startTime . "</td>";

				echo "</tr>";
			}
		}
		echo "</table>";

	}
	
	
	
	
	function createEvent ($client, $title, $desc, $where, $startDate, $startTime, $endDate, $endTime, $tzOffset)
	{
	  $gdataCal = new Zend_Gdata_Calendar($client);
	  $newEvent = $gdataCal->newEventEntry();
	  
	  $newEvent->title = $gdataCal->newTitle($title);
	  $newEvent->where = array($gdataCal->newWhere($where));
	  $newEvent->content = $gdataCal->newContent("$desc");
	  
	  $when = $gdataCal->newWhen();
	  $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
	  $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
	  $newEvent->when = array($when);
	
	  // Upload the event to the calendar server
	  // A copy of the event as it is recorded on the server is returned
	  $createdEvent = $gdataCal->insertEvent($newEvent);
	  return $createdEvent->id->text;
	}

	
}


?>
